#include "TextChunk.h"
//need this for null pointer
#include <cstdlib>
#include <fstream>
using namespace std;

//this MUST be in the CPP file or else linker errors
string TextChunk::defaultsFilename = "res/TextChunkDefaults.txt";


/*
 * NOTE:  ALL STRING INITIALIZATIONS IN THIS FILE ARE -COPY CONSTRUCTED-
 * IT IS SAFE AND NECESSARY TO DELETE STRINGS SENT AS ARGUMENTS TO
 * THESE FUNCTIONS
 */

/**
 * return internal string** array
 */
string** TextChunk::getAllStrings()
{
	return strArray;
}


TextChunk::TextChunk(string** ptrArray)
{
	title		 = new string(ptrArray[0][0].c_str());
	author		 = new string(ptrArray[1][0].c_str());
	description  = new string(ptrArray[2][0].c_str());
	copyright    = new string(ptrArray[3][0].c_str());
	creation_time= new string(ptrArray[4][0].c_str());
	software 	 = new string(ptrArray[5][0].c_str());
	disclaimer   = new string(ptrArray[6][0].c_str());
	source 		 = new string(ptrArray[7][0].c_str());
	comment		 = new string(ptrArray[8][0].c_str());
	// = new string[][] is not valid in C++
	//must loop to initialize the field
	strArray = new string*[9];
	for(int i = 0; i < 9; i++)
	{
		strArray[i] = new string[1];
	}
	
	//copy references into the array
	strArray[0][0] = *title;
	strArray[1][0] = *author;
	strArray[2][0] = *description;
	strArray[3][0] = *copyright;
	strArray[4][0] = *creation_time;
	strArray[5][0] = *software;
	strArray[6][0] = *disclaimer;
	strArray[7][0] = *source;
	strArray[8][0] = *comment;
}

TextChunk::TextChunk(string *TITLE, string *AUTHOR, string *DESCRIPTION,
			 string *COPYRIGHT, string *CREATION_TIME,
			 string *SOFTWARE, string *DISCLAIMER, 
			 string *SOURCE, string *COMMENT)
{
	title		 = new string(TITLE->c_str());
	author	     = new string(AUTHOR->c_str());
	description  = new string(DESCRIPTION->c_str());
	copyright	 = new string(COPYRIGHT->c_str());
	creation_time= new string(CREATION_TIME->c_str());
	software     = new string(SOFTWARE->c_str());
	disclaimer   = new string(DISCLAIMER->c_str());
	source		 = new string(SOURCE->c_str());
	comment		 = new string(COMMENT->c_str());
	
	strArray = new string*[9];
	for(int i = 0; i < 9; i++)
	{
		strArray[i] = new string[1];
	}
	
	//copy references into the array
	strArray[0][0] = *title;
	strArray[1][0] = *author;
	strArray[2][0] = *description;
	strArray[3][0] = *copyright;
	strArray[4][0] = *creation_time;
	strArray[5][0] = *software;
	strArray[6][0] = *disclaimer;
	strArray[7][0] = *source;
	strArray[8][0] = *comment;
}

/**
 * Delete all fields
 */
TextChunk::~TextChunk()
{
	delete title;
	delete author;
	delete description;
	delete copyright;
	delete creation_time;
	delete software;
	delete disclaimer;
	delete source;
	delete comment;
	
	/*
	 * Delete the array
	 * loop backwards
	 */
	for(int x = 0; x < 9; x++)
	{
		delete[] strArray[x];
	}
	delete[] strArray;
}

//FUNCTION IS STATIC
TextChunk TextChunk::readTextChunk(string* filename)
{
	string TITLE, AUTHOR, DESCRIPTION, COPYRIGHT, CREATION_TIME,
			SOFTWARE, DISCLAIMER, SOURCE, COMMENT;
			
	ifstream in(filename->c_str());
	
	//check if file is open, handle error
	if(!in.is_open())
	{
		
	}
	
	//initialize array to hold read values
	string** strArray = new string*[9];
	for(int i = 0; i < 9; i++)
	{
		strArray[i] = new string[1];
	}
	
	/*
	 * in order:
	 * TITLE
	 * AUTHOR
	 * DESCRIPTION
	 * COPYRIGHT
	 * CREATION_TIME
	 * SOFTWARE
	 * DISCLAIMER
	 * SOURCE
	 * COMMENT
	 */
	in >> strArray[0][0];
	in >> strArray[1][0];
	in >> strArray[2][0];
	in >> strArray[3][0];
	in >> strArray[4][0];
	in >> strArray[5][0];
	in >> strArray[6][0];
	in >> strArray[7][0];
	in >> strArray[8][0];
	
	TextChunk textChunk(strArray);
	
	/*
	 * Delete the array
	 * loop backwards
	 */
	for(int x = 0; x < 9; x++)
	{
		delete[] strArray[x];
	}
	delete[] strArray;
	if(in.is_open())
	{
		in.close();
	}
	
	return textChunk;
}

